package com.ecube.test;

import com.alibaba.fastjson.JSON;
import com.ecube.mqtt.MqttUtils;
import com.ecube.protocol.ProtocolHelper;
import com.ecube.protocol.common.DataReportHead;
import com.ecube.protocol.common.DataReportStandard;
import com.ecube.protocol.enums.WindFunctionEnum;
import com.ecube.protocol.wind.WindWpStationInfo;
import com.ecube.protocol.wind.WindWpStationState;
import com.ecube.utils.EnumUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 创建风能
 *
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/7/31 17:39
 */
public class SendWindWpStationInfo {


    public static void main(String[] args) throws Exception {
        // 风能站属性
        sendWindInfo("FAT", "100090022", "20250731");
        sendWindInfo("FAT", "100090022", "20250806");

        // 风能站状态
        while (true){
            sendState("FAT", "100090022", "20250731");
            sendState("FAT", "100090022", "20250806");
            Thread.sleep(1000*10);
        }
    }

    /**
     * 风能属性
     *
     * @param env env
     * @param productCode productCode
     * @param deviceCode deviceCode
     */
    public static void sendWindInfo(String env, String productCode, String deviceCode){
        WindWpStationInfo stationInfo = new WindWpStationInfo();
        WindFunctionEnum function = (WindFunctionEnum) EnumUtils.getFunction(stationInfo.getClass());
        if(function == null){
            return;
        }

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();
        DataReportStandard reportStandard = new DataReportStandard();
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportStandard.setHeader(reportHand);

        reportStandard.setDataBody(stationInfo);

        String reportContent = JSON.toJSONString(reportStandard);
        System.out.println("发送报文内容：" + reportContent);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);

        // 心跳空报文
        reportHand.setFunction("keepalive");
        DataReportStandard reportStandard1 = new DataReportStandard();
        reportStandard1.setHeader(reportHand);
        reportStandard1.setDataBody(new Object());
        MqttUtils.sendMQTT(env, pubTopic, JSON.toJSONString(reportStandard1));
    }

    /**
     * 风能状态
     *
     * @param env env
     * @param productCode productCode
     * @param deviceCode deviceCode
     */
    public static void sendState(String env, String productCode, String deviceCode) {
        WindFunctionEnum function = (WindFunctionEnum) EnumUtils.getFunction(WindWpStationState.class);
        if(function == null){
            return;
        }

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();
        DataReportStandard reportStandard = new DataReportStandard();
        reportStandard.setHeader(ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction()));

        WindWpStationState stationState = new WindWpStationState();
        reportStandard.setDataBody(stationState);

        String reportContent = JSON.toJSONString(reportStandard);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
    }
}
